package com.wjh.kt_vod.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjh.kt_vod.model.vod.Teacher;
import com.wjh.kt_vod.service.TeacherService;
import com.wjh.kt_vod.vo.vod.TeacherQueryVo;
import com.wjh.kt_vod.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "讲师管理接口")
@RestController
@RequestMapping(value = "/admin/vod/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * http://localhost:8301/admin/vod/teacher/findAll
     * 查询所有讲师
     *
     * @return
     */
    @ApiOperation(value = "查询所有讲师")
    @GetMapping(value = "/findAll")
    public Result findAll() {
        List<Teacher> list = teacherService.list();
        return Result.ok(list).message("数据查询成功");
    }

    /**
     * http://localhost:8301/admin/vod/teacher/remove/1
     * 删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "删除讲师")
    @DeleteMapping(value = "/remove/{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "ID", required = true)
                                 @PathVariable("id") long id) {
        boolean b = teacherService.removeById(id);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 待条件的分页查询
     * @param current
     * @param limit
     * @param teacherQueryVo
     * @return
     */
    @ApiOperation(value = "带条件的分页查询")
    @PostMapping(value = "/findQueryPage/{current}/{limit}")
    public Result findQuery(@PathVariable("current") Long current
            , @PathVariable("limit") Long limit
            , @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
        Page<Teacher> teacherPage = new Page<>(current, limit);
        if (StringUtils.isEmpty(teacherQueryVo)) {
            return Result.ok(teacherService.page(teacherPage, null));
        } else {
            String name = teacherQueryVo.getName();
            Integer levelteacher = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();
            QueryWrapper<Teacher> teacherQueryVoQueryWrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(name)) {
                teacherQueryVoQueryWrapper.like("name", name);
            }
            if (!StringUtils.isEmpty(levelteacher)) {
                teacherQueryVoQueryWrapper.eq("level", levelteacher);
            }
            if (!StringUtils.isEmpty(joinDateBegin)) {
                teacherQueryVoQueryWrapper.ge("join_date", joinDateBegin);
            }
            if (!StringUtils.isEmpty(joinDateEnd)) {
                teacherQueryVoQueryWrapper.le("join_date", joinDateEnd);
            }
            Page<Teacher> page = teacherService.page(teacherPage, teacherQueryVoQueryWrapper);
            return Result.ok(page);
        }

    }

    /**
     * 添加讲师
     *
     * @param teacher
     * @return
     */
    @ApiOperation(value = "添加讲师")
    @PostMapping(value = "/saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping(value = "/getTeacherById/{id}")
    public Result getTeacher(@PathVariable("id") long id) {
        Teacher byId = teacherService.getById(id);
        if (StringUtils.isEmpty(byId)) {
            return Result.fail();
        } else {
            return Result.ok(byId);
        }

    }

    /**
     * 修改讲师
     * @param teacher
     * @return
     */
    @ApiOperation(value = "修改讲师")
    @PutMapping(value = "/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.updateById(teacher);
        return Result.ok();
    }

    /**
     * http://localhost:8301/admin/vod/teacher/remove/1
     * 删除讲师
     *
     * @param idList
     * @return
     */
    @ApiOperation(value = "删除讲师idList")
    @DeleteMapping(value = "/removeByIdList")
    public Result removeTeacherByIdList(@RequestBody List<Long> idList) {
        boolean batch = teacherService.removeByIds(idList);
        if (batch){
            return Result.ok();
        }else {
            return Result.fail();
        }


    }


}
